﻿using System;

namespace LogNaga
{
	/// <summary>
	/// 要记录的错误项
	/// </summary>
	[Serializable]
	public class Error
	{
		/// <summary>
		/// 包含的异常，供需要记录格式化信息的Log使用
		/// </summary>
		public Exception Exception { get; private set; }

		/// <summary>
		/// 文本描述，供仅需要记录文本的Log使用
		/// </summary>
		public string ErrorMessage { get; set; }

		///<summary>
		///Log级别
		///</summary>
		public LogLevel LogLevel { get; private set; }

		///<summary>
		///Exception的产生时间
		///</summary>
		public DateTime CreatedTime { get; private set; }

		/// <summary>
		/// 程序名称
		/// </summary>
		public string ApplicationName
		{
			get
			{
				//TODO: 获取程序名称
				return string.Empty;
			}
			set//TODO: 为了支持xml序列化必须有set。需要测试是否设置为private set更合适
			{
				throw new NotSupportedException("applicationName");
			}
		}

		/// <summary>
		/// 机器名称
		/// </summary>
		public string MachineName
		{
			get
			{
				return Environment.MachineName;
			}
			set
			{
				throw new NotSupportedException("machineName");
			}
		}

		/// <summary>
		/// 创建一个<see cref="Error"/>实例
		/// </summary>
		/// <param name="exception"><see cref="Exception"/>实例</param>
		/// <param name="logLevel"><see cref="LogLevel"/>错误级别</param>
		public Error(Exception exception, LogLevel logLevel)
		{
			if (exception == null)
				throw new ArgumentNullException("exception");

			this.Exception = exception;
			this.LogLevel = logLevel;
			this.CreatedTime = DateTime.Now;
		}

		/// <summary>
		/// 创建一个<see cref="Error"/>实例
		/// </summary>
		/// <param name="errorMessage">要记录的信息内容</param>
		/// <param name="logLevel"><see cref="LogLevel"/>日志记录级别</param>
		public Error(string errorMessage, LogLevel logLevel)
		{
			if (string.IsNullOrEmpty(errorMessage))
				throw new ArgumentNullException("errorMessage");

			this.ErrorMessage = errorMessage;
			this.LogLevel = logLevel;
			this.CreatedTime = DateTime.Now;
		}
	}
}
